<?php

namespace app\api\mimiddleware;

use app\admin\common;
use app\admin\model\index\Admin;
use app\logic\traits\Token;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\Middleware;

class CheckToken extends Middleware
{
    public $key = 'fc6f2e74a75fd22a1224ef994c83011c';
    protected $method = 'HS256';
    use Token;
    public function handle($request, \Closure $next)
    {
        global $user;
        $token = $request->header('token');
        if(empty($token)){
            return $next($request);
        }
        try {
            list($code,$msg)=$this->GetToken($token); //获取token
        }catch (\Exception $exception){
            return common::return_json_layui([], $exception->getMessage(),-1000);
        }
        if(!$code){
            return common::return_json_layui([], $msg,-1000);
        }
        $request->user =$msg;
        $user=$msg;
        $request->user_id =$msg['id'];
        return $next($request);
    }
}